/********************************************************************************
 *@description: 红包记录
 *@author: Xhd
 *@API: 陈浩天
 *@date: 2024-02-23 09:32:02
 *@version: V1.0.0
**********************************************************************************/

import React, { Component } from 'react';
import TheQueryForm from '@/components/TheQueryForm';
import TheTable from '@/components/TheTable';
import ThePagination from '@/components/ThePagination';
import TheButton from '@/components/TheButton';
import { apiQueryRedPacketPage } from "@/request/wallet/record";
import store from "@/store/store";
import _ from "lodash";
import { getDateTime3 } from '@/utils/getDate';
import { exportSheetTable } from '@/utils/exportTable';
//本周
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const week = date.getDay();
let start = new Date(year,month,day - week + 1);
let end = new Date(year,month,day - week + 7);
class Envelope extends Component {
  constructor(props) {
    super(props)
    this.queryFormRef = React.createRef();
    this.state = {
      columns: [
        {
          title: "红包id",
          dataIndex: "sendRecordId",
          key: "sendRecordId",
        },
        {
          title: "发送用户昵称",
          dataIndex: "sendAccountName",
          key: "sendAccountName",
        },
        {
          title: "发送用户id",
          dataIndex: "sendAccountId",
          key: "sendAccountId",
        },
        {
          title: "发送方ip",
          dataIndex: "ipAddressAscription",
          key: "ipAddressAscription",
        },
        {
          title: "接收用户昵称",
          dataIndex: "receiveAccountName",
          key: "receiveAccountName",
        },
        {
          title: "接收用户id",
          dataIndex: "receiveAccountId",
          key: "receiveAccountId",
        },
        {
          title: "领取状态",
          dataIndex: "status",
          key: "status",
          render: (text) => {
            let status = ""
            switch (text) {
              case 0:
                status = "待领取"
                break;
              case 1:
                status = "已领取"
                break;
              case 2:
                status = "已退回"
                break;
              default:
                break;
            }
            return status
          }
        },
        {
          title: "金额",
          dataIndex: "sendMoney",
          key: "sendMoney",
          render: (text) => {
            return <React.$price price={text || 0} />
          }
        },
        {
          title: "红包备注",
          dataIndex: "remark",
          key: "remark",
        },
        {
          title: "发送时间",
          dataIndex: "createTimeToweb",
          key: "createTimeToweb",
        },
      ],
      dataSource: [],
      pageData: { pageNo: 1, pageSize: 10 },
      total: 0,
      queryForm: {
        // startQuery: getDateTime3(start) + ' 00:00:00',
        // endQuery: getDateTime3(end) + ' 23:59:59'
      },
      searchOption: [
        { field: 'time', label: '时间', placeholder: '请选择', required: false, rules: [], type: 'RangePicker' },
        { field: 'sendAccountId', label: '发送用户id', placeholder: '请输入发送用户id', required: false, rules: [], type: 'Input', defaultValue: '', },
        { field: 'sendMemberName', label: '发送用户昵称', placeholder: '请输入发送用户昵称', required: false, rules: [], type: 'Input', defaultValue: '', },
        { field: 'receiveAccountId', label: '接受用户id', placeholder: '请输入接受用户id', required: false, rules: [], type: 'Input', defaultValue: '', },
        { field: 'receiveMemberName', label: '接受用户昵称', placeholder: '请输入接受用户昵称', required: false, rules: [], type: 'Input', defaultValue: '', },
        { field: "status", label: "领取状态", placeholder: "全部", type: "Select", options: [{ value: '0', label: '待领取' }, { value: '1', label: '已领取' }, { value: '2', label: '已退回' }] },
        { field: 'sendRecordId', label: '红包id', placeholder: '请输入', required: false, rules: [], type: 'Input', defaultValue: '', },
      ],
    }
  }
  componentDidMount() {
    this.getData()
  }
  getData = async (values) => {
    if (values && !_.isEmpty(values.time)) {
      values.startQuery = getDateTime3(values.time[0]) + ' 00:00:00'
      values.endQuery = getDateTime3(values.time[1]) + ' 23:59:59'
      delete (values.time)
    }
    this.setState(
      {
        queryForm: values === undefined ? this.state.queryForm : { ...values },
      },
      async () => {
        const params = { ...this.state.queryForm, ...this.state.pageData, }
        store.dispatch({
          type: 'SaveSearchValues',
          data: { pathName: 'envelope', ...params }
        })
        console.log(params);
        let res = await apiQueryRedPacketPage({ ...params })
        if (res.code === 0) {
          this.setState({
            dataSource: res.data,
            pageData: {
              pageNo: res.pageNo,
              pageSize: res.pageSize
            },
            total: res.totalCount
          })
        }
      });
  };
  pageChange = (pageNo, pageSize) => {
    this.setState({
      pageData: { pageNo, pageSize, },
    }, () => {
      this.getData();
    }
    );
  };
  onReset = () => {
    this.setState({
      queryForm: {
        // startQuery: getDateTime3(start) + ' 00:00:00',
        // endQuery: getDateTime3(end) + ' 23:59:59'
      },
    },
      () => {
        this.getData();
      }
    );
  }
  onFormSearch = (values) => {
    this.setState(
      {
        pageData: { pageNo: 1, pageSize: 10 },
      },
      () => {
        this.getData(values);
      }
    );
  }
  toExport = async () => {
    const { queryForm } = this.state
    await apiQueryRedPacketPage({ ...queryForm, pageNo: 1, pageSize: -1 }).then(res => {
      if (res.code === 0 && res.data) {
        let th_arr = [
          {
            title: '红包往来记录',
            prop: '',
            children: [
              {
                title: '红包id',
                prop: 'sendRecordId'
              },
              {
                title: '发送用户昵称',
                prop: 'sendAccountName'
              },
              {
                title: '发送用户id',
                prop: 'sendAccountId'
              },
              {
                title: '发送方ip',
                prop: 'ipAddressAscription'
              },
              {
                title: '接收用户昵称',
                prop: 'receiveAccountName'
              },
              {
                title: '接收用户id',
                prop: 'receiveAccountId'
              },
              {
                title: '领取状态',
                prop: 'status'
              },
              {
                title: '金额',
                prop: 'sendMoney',
              },
              {
                title: "红包备注",
                prop: "remark",
              },
              {
                title: '发送时间',
                prop: 'createTimeToweb',
              },
            ]
          },
        ]
        const getStatus =  (text) => {
          let status = ""
          switch (text) {
            case 0:
              status = "待领取"
              break;
            case 1:
              status = "已领取"
              break;
            case 2:
              status = "已退回"
              break;
            default:
              break;
          }
          return status
        }
        let tb_arr = []
        res.data.map((item, index) => {
            tb_arr.push({
              sendRecordId: item.sendAccountName,
              sendAccountName: item.sendAccountName || '/',
              sendAccountId: item.sendAccountId || '/',
              ipAddressAscription: item.ipAddressAscription || '/',
              receiveAccountName: item.receiveAccountName || '/',
              receiveAccountId: item.receiveAccountId || '/',
              status:  getStatus(item.status),
              sendMoney: (item.sendMoney / 100).toFixed(2) || '/',
              remark: item.remark || '/',
              createTimeToweb: item.createTimeToweb || '/',
            })
        })
        const mergeProp = [
          'sendRecordId',
          'sendAccountName',
          'sendAccountId',
          'sendAccountId',
          'receiveAccountName',
          'receiveAccountId',
          'status',
          'sendMoney',
          'remark',
          'createTimeToweb',
        ]
        exportSheetTable({ fileName: '红包往来记录', tH_data: th_arr, tB_data: tb_arr, isSheet: true, merge: true, mergeProp })
      }
    })
  }
  render() {
    const { columns, dataSource, pageData: { pageNo, pageSize }, total, searchOption, } = this.state;
    return (<div className='p-layout-box'>
      <TheQueryForm ref={(ref) => (this.queryFormRef = ref)} show={true} option={searchOption} onReset={() => this.onReset()} onGetValues={(values) => this.onFormSearch(values)} >
        <TheButton haspermi={['wallet:envelope:export']} size="middle" type='default' onClick={() => this.toExport()} >导出</TheButton>

      </TheQueryForm>
      <TheTable scroll={{ x: 'max-content', y: '63vh' }} columns={columns} dataSource={dataSource} />
      <ThePagination pageNo={pageNo} pageSize={pageSize} total={total} pageChange={this.pageChange} />
    </div>)
  }
}

export default Envelope